package com.zhangkunhao.notes.service;

import com.zhangkunhao.notes.dao.BlackListDao;

import com.zhangkunhao.notes.entity.Do.BlackList;
import com.zhangkunhao.notes.entity.Do.User;
import com.zhangkunhao.notes.entity.bo.Message;
import com.zhangkunhao.notes.entity.bo.PageBean;
import com.zhangkunhao.notes.service.impl.BlackListService;

import java.sql.SQLException;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 用户黑名单接口的实现类
 *
 * @author 20642
 */
public class BlackListServiceImpl implements BlackListService {

    @Override
    public List<BlackList> selectAll() {
        return BlackListDao.getInstance().findAll();
    }

    @Override
    public void insert(BlackList blackList) throws Exception {
        BlackListDao.getInstance().insert(blackList);
    }

    @Override
    public int delete(int id) throws SQLException {
        return BlackListDao.getInstance().delete(id);
    }

    @Override
    public int update(BlackList blackList, String... params) throws Exception {
        return BlackListDao.getInstance().update(blackList);
    }

    /**
     * 显示我还没有拉黑的用户
     *
     * @param userId：
     * @return PageBean<User>
     */
    public PageBean<User> listOtherUsers(int userId) {
        //查询已经拉黑的用户的id
        List<Integer> blackUserIds = this.selectAll().stream()
                .filter(blackList -> blackList.getFromUserId().equals(userId))
                .map(BlackList::getForUserId)
                .collect(Collectors.toList());

        // 调用dao查询除了自己之外的用户
        List<User> userList = new UserServiceImpl().selectAll()
                .stream()
                .filter(user1 -> (!user1.getId().equals(userId)))
                .collect(Collectors.toList());

        // 迭代器，过滤已经拉黑的用户
        Iterator<User> it = userList.iterator();
        //过滤
        while (it.hasNext()) {
            User user = it.next();
            for (Integer blackUserId : blackUserIds) {
                if (user.getId().equals(blackUserId)) {
                    //不能是userList.remove(user)
                    it.remove();
                    break;
                }
            }
        }
        //返回数据
        return new PageBean<>(userList.size(), userList);
    }

    /**
     * 查询我拉黑的用户
     *
     * @param userId：
     * @return PageBean<User>
     */
    public PageBean<User> listBlackUsers(int userId) {
        //查询已经拉黑的用户的id
        List<Integer> blackUserIds = this.selectAll().stream()
                .filter(blackList -> blackList.getFromUserId().equals(userId))
                .map(BlackList::getForUserId)
                .collect(Collectors.toList());

        // 调用dao查询除了自己之外的用户
        List<User> userList = new UserServiceImpl().selectAll()
                .stream()
                .filter(user1 -> (!user1.getId().equals(userId)))
                .collect(Collectors.toList());

        //迭代器， 过滤掉没有拉黑的用户
        Iterator<User> it = userList.iterator();

        //是否存在黑名单内
        boolean isBlackList;

        while (it.hasNext()) {
            isBlackList = false;
            User user = it.next();
            for (Integer blackUserId : blackUserIds) {
                if (user.getId().equals(blackUserId)) {
                    isBlackList = true;
                    break;
                }
            }
            if (!isBlackList) {
                //不能是userList.remove(user)
                it.remove();
            }
        }

        //返回数据
        return new PageBean<>(userList.size(), userList);
    }

    /**
     * 拉黑其他用户
     *
     * @param userId：
     * @param deFriendId：被拉黑用户的id
     * @return Message<BlackList>
     * @throws Exception:
     */
    public Message<BlackList> deFriend(int userId, int deFriendId) throws Exception {
        //初始化
        Message<BlackList> blackListMessage = new Message<>(null, "拉黑该用户失败", 0);

        //调用dao
        this.insert(new BlackList(null, userId, deFriendId));

        //提供判断
        BlackList blackList = this.selectAll()
                .stream()
                .filter(blackList3 -> blackList3.getFromUserId().equals(userId) && blackList3.getForUserId().equals(deFriendId))
                .collect(Collectors.toList()).get(0);

        //成功
        if (blackList != null) {
            blackListMessage.setResult("将该用户拉入黑名单成功");
            blackListMessage.setStatus(1);
            blackListMessage.setT(blackList);
        }

        //返回数据
        return blackListMessage;
    }

    /**
     * 取消拉黑
     *
     * @param userId:
     * @param cancelDeFriendId:取消拉黑的用户的id
     * @return Message<BlackList>
     * @throws Exception:
     */
    public Message<BlackList> cancelDeFriend(int userId, int cancelDeFriendId) throws Exception {

        //初始化
        Message<BlackList> blackListMessage = new Message<>(null, "取消拉黑失败", 0);

        //调用dao
        BlackList blackList = this.selectAll()
                .stream()
                .filter(blackList1 -> blackList1.getFromUserId().equals(userId) && blackList1.getForUserId().equals(cancelDeFriendId))
                .collect(Collectors.toList()).get(0);
        //调用dao
        int delete = this.delete(blackList.getId());

        //成功
        if (delete > 0) {
            blackListMessage.setResult("取消拉黑成功");
            blackListMessage.setStatus(1);
        }

        //返回数据
        return blackListMessage;
    }
}
